package com.yc.cloud.common.websocket.core;

import com.corundumstudio.socketio.SocketIOClient;
import com.yc.cloud.common.websocket.service.SocketService;
import lombok.val;

/**
 * 消息异步处理
 */
public class SocketMessageProcessor implements Runnable {

    private final String event;

    private final SocketIOClient socketIOClient;

    private final SocketService socketService;

    public SocketMessageProcessor(SocketService socketService, String event, SocketIOClient socketIOClient) {
        this.socketService = socketService;
        this.event = event;
        this.socketIOClient = socketIOClient;
    }

    @Override
    public void run() {
        switch (event) {
            //有人加入或者离开都通知在线用户
            case SocketMessage.USER_CONNECTED:
            case SocketMessage.USER_DIS_CONNECTED:
                val onLineUsers = socketService.getOnLineUsers();
                socketService.broadcastAllOnlineUsers(socketIOClient, SocketMessage.SYNC_ONLINE_USERS, onLineUsers);
                break;
            case SocketMessage.GET_SERVER_TIME:
        }
    }

    private void broadcastAllOnlineUsers() {
        val playerVo = SocketClientInfoManager.getInstance().convertPlayerVo(socketIOClient);
        //通知再线成员的用户,有用户加入或者离开
        socketService.broadcastAllOnlineUsers(socketIOClient, event, playerVo);
    }


}
